<?php
/**
 * DisorderController class file.
 *
 * @package   controllers
 * @since     1.0
 * @filesource
 */

/**
 * Controller for disorder actions. Includes CRUD for Disorders.
 * Controller class is also responsible for rendering page.
 */
class DisorderController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';
		
	/**
	 * @var string the name of the default action. In this case, admin is the default action, meaning
	 * when request the controller not specifying the action, the action that will be called.
	 */
	public $defaultAction='admin';

	/**
	 * Return a list of filters that will be used to control access.
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter which is declared in $this->filters().
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow admin user to perform 'create','admin', 'delete' and 'update' actions
				'actions'=>array('create','admin','update','delete'),
				'roles'=>array('admin'),
			),
			array('deny',  // deny all other users
				'users'=>array('*'),
			),
		);
	}


	/**
	 * Creates a new disorder.
	 * This action will display a form for creating a disorder if no data posted back.
	 * If data posted back, it will validate the data and try to save it to database.
	 * If creation is successful, the browser will be redirected to the 'admin' page.
	 */
	public function actionCreate()
	{
		$model=new Disorder;

		if(isset($_POST['Disorder']))
		{
			$model->attributes=$_POST['Disorder'];
			if($model->save())
				$this->redirect(array('admin'));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	
	/**
	 * Updates a particular disorder.
	 * This action will display a form for updating a disorder if no data posted back.
	 * If data posted back, it will validate the data and try to save it to database.
	 * If update is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the disorder to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['Disorder']))
		{
			$model->attributes=$_POST['Disorder'];
			if($model->save())
				$this->redirect(array('admin'));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}


	/**
	 * Deletes a particular disorder.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the disorder to be deleted
	 * @throws CHttpException if the request is not POST request.
	 */
	public function actionDelete($id)
	{
		// we only allow deletion via POST request
		if(Yii::app()->request->isPostRequest)
		{
			$this->loadModel($id)->delete();
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}


	/**
	 * Manages all disorders.
	 * This action will render an admin page using Disorder's method search() to produce a list of disorders.
	 * This method will produce a list of disorder by search conditions.
	 */
	public function actionAdmin()
	{
		$model=new Disorder('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Disorder']))
			$model->attributes=$_GET['Disorder'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the disorder object based on the primary key given in the GET variable.
	 * @param integer the ID of the disorder to be loaded
	 * @return Disorder
	 * @throws CHttpException if the disorder object is not found
	 */
	public function loadModel($id)
	{
		$model=Disorder::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}